<template>
  <div>
    <div>
    <router-link to="/">登录</router-link> |
    <router-link to="/register">注册</router-link>
    </div>
    <div id="login">
      <el-card class="box-card" shadow="hover">
        <div slot="header" class="clearfix">
          <span>用户登录</span>
        </div>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="用户名">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input placeholder="请输入密码" v-model="form.password" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit" style="logBtn">立即登录</el-button>
          </el-form-item>
        </el-form>
        <span>没有账号？<router-link to="/register">去注册</router-link></span>
      </el-card>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'login',
  data() {
    return {
      form: {
        name: '',
        password: ''
      },
      errMsg: ' ',
      suMsg: ''
    }
  },
  methods: {
    onSubmit() {
      const that = this
      // console.log('登录成功!')
      console.log(this.form.name)
      axios
        .post('http://47.110.234.216:7001/login', {
          username: this.form.name,
          password: this.form.password
        })
        .then(function (res) {
          console.log(res.data.status, 'hi')
          that.errMsg = '登录失败'

          // eslint-disable-next-line eqeqeq
          if (res.data.status == true) {
            that.$message({
              showClose: true,
              message: '登陆成功',
              type: 'success'
            })
            that.$router.push('/table')
            return
          }
          that.$message({
            showClose: true,
            message: that.errMsg,
            type: 'error'
          })
        })
    }
  }
}

</script>

<style scoped>
  .nav {
    width: 350px;
    height: 400px;
  }

  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }

  .clearfix:after {
    clear: both;
  }

  .box-card {
    width: 480px;
    margin: auto;
  }

  .logBtn {
    margin-bottom: 20px;
  }

  a {
    text-decoration: none;
    color: orange;
  }

  .router-link-active {
    text-decoration: none;
  }

  </style>
